<template>
  <div class="register-container">
    <!-- 注册成功toast -->
    <div v-if="showToast" class="success-toast">
      {{ toastMsg }}
    </div>
    <!-- 状态栏 -->
    <div class="status-bar">
      <span class="time">12:00</span>
      <div class="status-icons">
        <span class="signal"></span>
        <span class="wifi"></span>
        <span class="battery"></span>
      </div>
    </div>
    <!-- 返回按钮 -->
    <div class="header">
      <div class="back-btn" @click="goBack">&lt;</div>
    </div>
    <!-- 标题 -->
    <div class="title-section">
      <h1 class="main-title">快速注册</h1>
      <p class="subtitle">未注册手机号将自动注册并登录</p>
    </div>
    <!-- 注册表单 -->
    <div class="register-form">
      <div class="input-group">
        <div class="input-icon"><div class="phone-icon"></div></div>
        <div class="input-wrapper">
          <span class="country-code">+86</span>
          <input type="text" v-model="phoneNumber" placeholder="请输入11位手机号" class="phone-input" maxlength="11">
        </div>
      </div>
      <div class="input-group">
        <div class="input-icon"><div class="lock-icon"></div></div>
        <div class="input-wrapper">
          <input type="text" v-model="verificationCode" placeholder="请输入验证码" class="code-input" maxlength="6">
          <button class="get-code-btn" @click="getVerificationCode" :disabled="!canGetCode || countdown > 0">
            {{ countdown > 0 ? `${countdown}s` : '获取' }}
          </button>
        </div>
      </div>
      <div class="help-link">
        <a href="#" @click.prevent="handleCannotReceiveCode">收不到验证码</a>
      </div>
      <button class="register-btn" @click="handleRegister" :disabled="!canRegister">注册</button>
    </div>
    <!-- 协议同意 -->
    <div class="agreement">
      <label class="checkbox-container">
        <input type="checkbox" v-model="agreed" class="checkbox">
        <span class="checkmark"></span>
      </label>
      <span class="agreement-text">
        我已阅读并同意
        <a href="#" class="link">AI智能医疗辅助系统 用户协议、隐私政策以及儿童个人信息保护政策</a>
      </span>
    </div>
    <!-- 验证码横幅 -->
    <div v-if="showDevCode" class="dev-code-banner">
      【健康医疗】您的验证码：{{ devCodeMsg.split(':')[1]?.trim() }}，请勿泄露。
    </div>
    <!-- 底部手势条 -->
    <div class="gesture-bar"></div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'

export default defineComponent({
  name: 'Register',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const phoneNumber = ref('')
    const verificationCode = ref('')
    const agreed = ref(false)
    const countdown = ref(0)
    const loading = ref(false)
    
    // 页面加载时检查URL参数
    onMounted(() => {
      if (route.query.phone) {
        phoneNumber.value = route.query.phone as string
      }
    })
    // toast相关
    const toastMsg = ref('')
    const showToast = ref(false)
    let toastTimer: any = null
    function showSuccessToast(msg: string) {
      toastMsg.value = msg
      showToast.value = true
      if (toastTimer) clearTimeout(toastTimer)
      toastTimer = setTimeout(() => {
        showToast.value = false
      }, 2000)
    }
    // 验证码横幅相关
    const devCodeMsg = ref('')
    const showDevCode = ref(false)
    let devCodeTimer: any = null

    const canGetCode = computed(() => {
      return phoneNumber.value.length === 11 && /^1[3-9]\d{9}$/.test(phoneNumber.value)
    })
    const canRegister = computed(() => {
      return phoneNumber.value.length === 11 && verificationCode.value.length === 6 && agreed.value
    })

    // 获取验证码
    const getVerificationCode = async () => {
      if (!canGetCode.value) {
        showSuccessToast('请输入正确的手机号')
        return
      }
      try {
        loading.value = true
        const response = await axios.post('http://localhost:3000/users/send-code', {
          phoneNumber: phoneNumber.value
        })
        if (response.data.success) {
          devCodeMsg.value = `健康医疗验证码: ${response.data.code}`
          showDevCode.value = true
          if (devCodeTimer) clearTimeout(devCodeTimer)
          devCodeTimer = setTimeout(() => {
            showDevCode.value = false
          }, 4000)
          countdown.value = 60
          const timer = setInterval(() => {
            countdown.value--
            if (countdown.value <= 0) clearInterval(timer)
          }, 1000)
        } else {
          showSuccessToast(response.data.message)
        }
      } catch (error: any) {
        showSuccessToast(error.response?.data?.message || '发送验证码失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }

    // 注册处理
    const handleRegister = async () => {
      if (!canRegister.value) {
        showSuccessToast('请填写完整信息并同意协议')
        return
      }
      try {
        loading.value = true
        const response = await axios.post('http://localhost:3000/users/register', {
          phoneNumber: phoneNumber.value,
          verificationCode: verificationCode.value
        })
        if (response.data.success) {
          showSuccessToast('注册成功')
          setTimeout(() => {
            router.push({ 
              path: '/verification-login', 
              query: { phone: phoneNumber.value } 
            })
          }, 1200)
        } else {
          showSuccessToast(response.data.message)
        }
      } catch (error: any) {
        showSuccessToast(error.response?.data?.message || '注册失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
    const goBack = () => {
      router.back()
    }
    const handleCannotReceiveCode = () => {
      showSuccessToast('请联系客服处理')
    }
    return {
      phoneNumber,
      verificationCode,
      agreed,
      countdown,
      loading,
      canGetCode,
      canRegister,
      getVerificationCode,
      handleRegister,
      goBack,
      handleCannotReceiveCode,
      // toast
      toastMsg,
      showToast,
      // dev code
      devCodeMsg,
      showDevCode
    }
  }
})
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  background: #fff;
  position: relative;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 44px;
  background: #ffffff;
}
.time {
  font-size: 14px;
  font-weight: 600;
  color: #000;
}
.status-icons {
  display: flex;
  align-items: center;
  gap: 4px;
}
.signal, .wifi, .battery {
  width: 20px;
  height: 12px;
  background: #000;
  border-radius: 2px;
}
.header {
  display: flex;
  align-items: center;
  padding: 0 20px;
  margin-bottom: 10px;
}
.back-btn {
  font-size: 24px;
  color: #333;
  cursor: pointer;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
.title-section {
  text-align: center;
  margin-bottom: 40px;
}
.main-title {
  font-size: 24px;
  font-weight: 600;
  color: #000;
  margin: 0 0 8px 0;
}
.subtitle {
  font-size: 14px;
  color: #8E8E93;
  margin: 0;
}
.register-form {
  margin-bottom: 30px;
  padding: 0 20px;
}
.input-group {
  display: flex;
  align-items: center;
  background: #F2F2F7;
  border-radius: 12px;
  padding: 12px 16px;
  margin-bottom: 16px;
}
.input-icon {
  margin-right: 12px;
}
.phone-icon {
  width: 20px;
  height: 20px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%238E8E93"><path d="M17,19H7V5H17M17,1H7C5.89,1 5,1.89 5,3V21A2,2 0 0,0 7,23H17A2,2 0 0,0 19,21V3C19,1.89 18.1,1 17,1Z"/></svg>') no-repeat center;
  background-size: contain;
}
.lock-icon {
  width: 20px;
  height: 20px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%238E8E93"><path d="M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"/></svg>') no-repeat center;
  background-size: contain;
}
.input-wrapper {
  display: flex;
  align-items: center;
  flex: 1;
}
.country-code {
  color: #007AFF;
  font-weight: 500;
  margin-right: 8px;
}
.phone-input,
.code-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 16px;
  color: #000;
  outline: none;
}
.phone-input::placeholder,
.code-input::placeholder {
  color: #8E8E93;
}
.get-code-btn {
  background: #007AFF;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.get-code-btn:disabled {
  background: #C7C7CC;
  cursor: not-allowed;
}
.help-link {
  text-align: right;
  margin-top: 0;
  margin-bottom: 16px;
}
.help-link a {
  color: #007AFF;
  text-decoration: none;
  font-size: 14px;
}
.register-btn {
  width: 100%;
  height: 50px;
  background: #007AFF;
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 16px;
  transition: background-color 0.2s;
}
.register-btn:disabled {
  background: #C7C7CC;
  cursor: not-allowed;
}
.agreement {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 40px;
  padding: 0 20px;
  font-size: 12px;
  color: #8E8E93;
}
.checkbox-container {
  position: relative;
  display: inline-block;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  margin-top: 2px;
}
.checkbox {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 20px;
  width: 20px;
  background-color: #fff;
  border: 2px solid #C7C7CC;
  border-radius: 4px;
  transition: all 0.2s;
}
.checkbox:checked ~ .checkmark {
  background-color: #007AFF;
  border-color: #007AFF;
}
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}
.checkbox:checked ~ .checkmark:after {
  display: block;
}
.checkbox-container .checkmark:after {
  left: 6px;
  top: 2px;
  width: 4px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
.agreement-text {
  font-size: 12px;
  color: #8E8E93;
  line-height: 1.4;
}
.link {
  color: #007AFF;
  text-decoration: none;
}
.link:hover {
  text-decoration: underline;
}
.dev-code-banner {
  position: fixed;
  left: 50%;
  bottom: 80px;
  transform: translateX(-50%);
  min-width: 220px;
  max-width: 80vw;
  background: #fff;
  color: #222;
  text-align: center;
  padding: 14px 24px;
  border-radius: 18px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.18), 0 1.5px 4px rgba(0,0,0,0.10);
  font-size: 16px;
  letter-spacing: 1px;
  z-index: 9999;
  border: 1px solid #e5e5e5;
  animation: sms-pop 0.3s;
}
@keyframes sms-pop {
  0% { opacity: 0; transform: translateX(-50%) translateY(30px) scale(0.95);}
  100% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1);}
}
.success-toast {
  position: fixed;
  left: 50%;
  bottom: 80px;
  transform: translateX(-50%);
  min-width: 180px;
  max-width: 80vw;
  background: #22c55e;
  color: #fff;
  text-align: center;
  padding: 14px 24px;
  border-radius: 18px;
  box-shadow: 0 4px 24px rgba(34,197,94,0.18), 0 1.5px 4px rgba(0,0,0,0.10);
  font-size: 16px;
  letter-spacing: 1px;
  z-index: 9999;
  animation: sms-pop 0.3s;
}
.gesture-bar {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 134px;
  height: 5px;
  background: #000;
  border-radius: 3px;
  margin-bottom: 8px;
}
</style> 